package com.nightsoul.cxf;

import javax.xml.ws.Endpoint;

import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.interceptor.OutFaultChainInitiatorObserver;
import org.apache.cxf.jaxrs.JAXRSBindingFactory;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.junit.After;
import org.junit.Test;

import com.nightsoul.cxf.interceptor.in.InvokeInterceptor;
import com.nightsoul.cxf.interceptor.in.PostLogicalInterceptor;
import com.nightsoul.cxf.interceptor.in.PostProtocolInterceptor;
import com.nightsoul.cxf.interceptor.in.PostStreamInterceptor;
import com.nightsoul.cxf.interceptor.in.PreInvokeInterceptor;
import com.nightsoul.cxf.interceptor.in.PreLogicalInterceptor;
import com.nightsoul.cxf.interceptor.in.PreProtocolInterceptor;
import com.nightsoul.cxf.interceptor.in.PreStreamInterceptor;
import com.nightsoul.cxf.interceptor.in.ReadInterceptor;
import com.nightsoul.cxf.interceptor.in.ReceiveInterceptor;
import com.nightsoul.cxf.interceptor.in.UnmarshalInterceptor;
import com.nightsoul.cxf.interceptor.in.UserLogicalInterceptor;
import com.nightsoul.cxf.interceptor.in.UserProtocolInterceptor;
import com.nightsoul.cxf.interceptor.in.UserStreamInterceptor;
import com.nightsoul.cxf.interceptor.out.MarshalInterceptor;
import com.nightsoul.cxf.interceptor.out.PrepareSendInterceptor;
import com.nightsoul.cxf.interceptor.out.SendInterceptor;
import com.nightsoul.cxf.interceptor.out.SetupInterceptor;
import com.nightsoul.cxf.interceptor.out.WriteInterceptor;

public class ServerTest {
	public static final String ADDRESS = "http://127.0.0.1:9000/hello";
	
	@Test
	public void testEndpoint() throws Exception {
		Endpoint.publish(ADDRESS, new HelloServiceImpl());
	}
	
	@Test
	public void testJaxWsServerFactoryBean() throws Exception {
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		factoryBean.setAddress(ADDRESS);
		factoryBean.setServiceClass(HelloService.class);
		factoryBean.setServiceBean(new HelloServiceImpl());
		factoryBean.getInInterceptors().add(new ReceiveInterceptor());
		factoryBean.getInInterceptors().add(new PreStreamInterceptor());
		factoryBean.getInInterceptors().add(new UserStreamInterceptor());
		factoryBean.getInInterceptors().add(new PostStreamInterceptor());
		factoryBean.getInInterceptors().add(new ReadInterceptor());
		factoryBean.getInInterceptors().add(new PreProtocolInterceptor());
		factoryBean.getInInterceptors().add(new UserProtocolInterceptor());
		factoryBean.getInInterceptors().add(new PostProtocolInterceptor());
		factoryBean.getInInterceptors().add(new UnmarshalInterceptor());
		factoryBean.getInInterceptors().add(new PreLogicalInterceptor());
		factoryBean.getInInterceptors().add(new UserLogicalInterceptor());
		factoryBean.getInInterceptors().add(new PostLogicalInterceptor());
		factoryBean.getInInterceptors().add(new PreInvokeInterceptor());
		factoryBean.getInInterceptors().add(new SecondInterceptor());
		factoryBean.getInInterceptors().add(new InvokeInterceptor());
		factoryBean.getInInterceptors().add(new MyPreInvokeInterceptor());
		
		
		
		factoryBean.getOutInterceptors().add(new SetupInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PreLogicalInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.UserLogicalInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PostLogicalInterceptor());
		factoryBean.getOutInterceptors().add(new PrepareSendInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PreStreamInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PreProtocolInterceptor());
		factoryBean.getOutInterceptors().add(new WriteInterceptor());
		factoryBean.getOutInterceptors().add(new MarshalInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.UserProtocolInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PostProtocolInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.UserStreamInterceptor());
		factoryBean.getOutInterceptors().add(new com.nightsoul.cxf.interceptor.out.PostStreamInterceptor());
		factoryBean.getOutInterceptors().add(new SendInterceptor());
		factoryBean.getOutInterceptors().add(new MyPostInvokeInterceptor());
		factoryBean.create();
	
		OutFaultChainInitiatorObserver ofio;
	}
	
	@Test
	public void testSimpleFrontEnd() {
		ServerFactoryBean factoryBean = new ServerFactoryBean();
		factoryBean.setAddress(ADDRESS);
		factoryBean.setServiceClass(HelloService.class);
		factoryBean.setServiceBean(new HelloServiceImpl());
		factoryBean.create();
	}
	
	@Test
	public void testSpring() throws Exception {
		SpringBusFactory factory = new SpringBusFactory();
		factory.createBus();
	}
	
	
	@Test
	public void testJaxRsServerFactoryBean() throws Exception {
		JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
		factoryBean.setResourceClasses(HelloServiceImpl.class);
		factoryBean.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);
		factoryBean.setAddress(ADDRESS);
		factoryBean.getInInterceptors().add(new TestInterceptor());
		factoryBean.getInInterceptors().add(new SecondInterceptor());
		factoryBean.create();
	}
	
	
	@After
	public void after() throws Exception {
		Object object = new Object();
		synchronized (object) {
			object.wait();
		}
	}
}
